package com.bjpowernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjpowernode.mapper.CommentsMapper;
import com.bjpowernode.domain.Comments;
import com.bjpowernode.service.CommentsService;
import org.springframework.util.StringUtils;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;

    /**
     * 根据房子id分页获取房屋评论
     *
     * @param id
     * @param page
     * @return
     */
    @Override
    public Page<Comments> findCommentsByPage(Integer id, Integer page) {
        Page<Comments> commentsPage = new Page<>(page, 5);
        // 查询房屋信息
        Page<Comments> selectPage = commentsMapper.selectPage(commentsPage, new LambdaQueryWrapper<Comments>() {
                }
                        .eq(Comments::getHouseid, id)
                        .orderByDesc(Comments::getStar)
        );
        // 获取用户信息进行脱敏处理.
        List<Comments> records = commentsPage.getRecords();
        records.forEach(comments -> {
            String username = comments.getUsername();
            if (StringUtils.hasText(username)) {
                StringBuilder stringBuilder = new StringBuilder(username);
                StringBuilder replace = stringBuilder.replace(3, 7, "****");
                comments.setUsername(replace.toString());
            }
        });
        return selectPage;
    }
}
